<?php

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @version    $Id: HeadLink.php 16971 2009-07-22 18:05:45Z mikaelkael $
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
/** Zend_View_Helper_Placeholder_Container_Standalone */
// require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';

/**
 * Zend_Layout_View_Helper_HeadLink
 *
 * @see        http://www.w3.org/TR/xhtml1/dtds.html
 * @uses       Zend_View_Helper_Placeholder_Container_Standalone
 * @package    Zend_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Core_View_Helper_HeadLink extends Zend_View_Helper_HeadLink {

    /**
     * @var string registry key
     */
    protected $_regKey = 'Zend_View_Helper_HeadLink';
    /**
     * 
     * The folder to be appended to the base url to find minify on your server.
     * The default assumes you installed minify in your documentroot\min directory
     * if you modified the directory name at all, you need to let the helper know 
     * here.
     * @var string
     */
    protected $_minifyLocation = '/externals/minify/';
    /**
     * 
     * Known Valid CSS Extension Types
     * @var array
     */
    protected $_cssExtensions = array(".css", ".css1", ".css2", ".css3");

    /**
     * headLink() - View Helper Method
     *
     * Returns current object instance. Optionally, allows passing array of
     * values to build link.
     *
     * @return Zend_View_Helper_HeadLink
     */
    public function headLink(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND) {
        if (null !== $attributes) {
            $item = $this->createData($attributes);
            switch ($placement) {
                case Zend_View_Helper_Placeholder_Container_Abstract::SET:
                    $this->set($item);
                    break;
                case Zend_View_Helper_Placeholder_Container_Abstract::PREPEND:
                    $this->prepend($item);
                    break;
                case Zend_View_Helper_Placeholder_Container_Abstract::APPEND:
                default:
                    $this->append($item);
                    break;
            }
        }
        return $this;
    }

    /**
     * 
     * Gets a string representation of the headLinks suitable for inserting
     * in the html head section. 
     * 
     * It is important to note that the minified files will be minified
     * in reverse order of being added to this object, and ALL files will be rendered
     * prior to inline being rendered.
     *
     * @see Zend_View_Helper_HeadScript->toString()
     * @param  string|int $indent
     * @return string
     */
    
     public function toStringDev($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->getWhitespace($indent)
                : $this->getIndent();

        $items = array();
        $this->getContainer()->ksort();
        foreach ($this as $item) {
            $items[] = $this->itemToString($item);
        }
        $t= $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
        return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
    }

    
    public function toString($indent = null) {

        if(APPLICATION_ENV == 'development' || !Engine_Api::_()->hasModuleBootstrap('minify')){
    		return $this->toStringDev($indent);
    	}
        //get mincss mode
        $settings = Engine_Api::_()->getApi('settings', 'core');
        $values = $settings->minify;
        if($values['mincss']['enable']==0)
        {
            return $this->toStringDev($indent);
            
        }
        // The base URL
        $baseUrl = $this->getBaseUrl();
        //remove the slash at the beginning if there is one  
        if (substr($baseUrl, 0, 1) == '/') {
            $baseUrl = substr($baseUrl, 1);
        }

        $indent = (null !== $indent) ? $this->getWhitespace($indent) : $this->getIndent();

        $items = array();
        $stylesheets = array();
        $this->getContainer()->ksort();
        foreach ($this as $item) {
            if ($item->type == 'text/css' && $item->conditionalStylesheet === false && strpos($item->href, 'http://') === false && $this->isValidStyleSheetExtension($item->href)) {
                $stylesheets [$item->media] [] = str_replace($this->getBaseUrl(), '', $item->href);
            } else {
                // first get all the stylsheets up to this point, and get them into
                // the items array
                $seen = array();
                foreach ($stylesheets as $media => $styles) {
                    $minStyles = new stdClass();
                    $minStyles->rel = 'stylesheet';
                    $minStyles->type = 'text/css';
                    $styles = $this->cleanURLForMinify($styles);
                    if (is_null($baseUrl) || $baseUrl == '') {
                        $minStyles->href = $this->getMinUrl() . '?f=' . implode(',', $styles);
                    } else {
                        $minStyles->href = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $styles);
                    }

                    $minStyles->media = $media;
                    $minStyles->conditionalStylesheet = false;
                    if (in_array($this->itemToString($minStyles), $seen)) {
                        continue;
                    }
                    $items [] = $this->itemToString($minStyles); // add the minified item
                    $seen [] = $this->itemToString($minStyles); // remember we saw it
                }
                $stylesheets = array(); // Empty our stylesheets array
                $items [] = $this->itemToString($item); // Add the item
            }
        }

        // Make sure we pick up the final minified item if it exists.
        $seen = array();
        foreach ($stylesheets as $media => $styles) {
            $minStyles = new stdClass();
            $minStyles->rel = 'stylesheet';
            $minStyles->type = 'text/css';

            $styles = $this->cleanURLForMinify($styles);
            if (is_null($baseUrl) || $baseUrl == '') {
                $minStyles->href = $this->getMinUrl() . '?f=' . implode(',', $styles);
            } else {
                $minStyles->href = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $styles);
            }
            $minStyles->media = $media;
            $minStyles->conditionalStylesheet = false;
            if (in_array($this->itemToString($minStyles), $seen)) {
                continue;
            }
            $items [] = $this->itemToString($minStyles);
            $seen [] = $this->itemToString($minStyles);
        }
        $t = $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
        return $indent . implode($this->_escape($this->getSeparator()) . $indent, $items);
    }

    /**
     * 
     * Loops through the defined valid static css extensions we use.
     * @param string $string
     */
    public function isValidStyleSheetExtension($string) {
        //return true;
        $string = $this->cleanURLForMinify(array($string));
        $string = $string[0];
        foreach ($this->_cssExtensions as $ext) {
            if (substr_compare($string, $ext, -strlen($ext), strlen($ext)) === 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 
     */
    public function getBaseUrl() {
        return Zend_Controller_Front::getInstance()->getBaseUrl();
    }

    /**
     * Retrieve the minify url
     *
     * @return string
     */
    public function getMinUrl() {
        return $this->getBaseUrl() . $this->_minifyLocation;
    }

    /**
     * Retrieve array url
     *
     * @return array
     */
    public function cleanURLForMinify($urls) {
        $clean = array();
        $pattern = array('/(\?.*)/', '/\/\//');
        $replacement = array('', '');

        foreach ($urls as $url) {
            $clean[] = ltrim(preg_replace($pattern, $replacement, $url), " /");
        }
        return $clean;
    }

}
